import re
import time

import requests
from lxml import etree
import pymysql
from bs4 import BeautifulSoup

url = 'http://www.iupian.com/top.html'
#
# con = pymysql.connect(host="localhost", port=3306, user="root", password="123456")
# con.select_db("movie")
# cur = con.cursor()
#
# res = requests.get(url)
# res = res.content.decode('utf8')
# titles = re.findall(r'<h2><a target="_blank" href=".*?">(.*?)</a></h2>', res)
# for title in titles:
#     cur.executemany("insert into youpiantitle (title) values (%s)", title)
#     print(title)
# #
# cur.close()
# con.close()


res = requests.get(url)
res = res.content.decode()
# print(res)

soup = BeautifulSoup(res, features='lxml')

beautiful_soup = soup.prettify()
print(beautiful_soup)
html_tree = etree.HTML(beautiful_soup)
print(html_tree)
movie_datas = html_tree.xpath("//a[@class='list-img']")
# print(movie_datas)
# # # print(movie_datas)
for movie_data in movie_datas:
    title = movie_data.attrib.get("title")
    movie_img = movie_data.xpath(".//img")[0]
    img_src = movie_img.attrib.get("data-url")
    print(title, img_src)
    time.sleep(1)
